<?php

namespace app\common\model;

use think\Model;

class Support extends Model
{
    protected $autoWriteTimestamp = true;
    public function userSupportPost()
    {
        $param = request()->param();
        $userId =  request()->userId;
        // 判断是否之前点赞过
        $support =  $this->where(['user_id' => $userId, 'post_id' => $param['post_id']])->find();
        if ($support) {

            if ($support['type'] == $param['type']) TApiException('请勿重复操作', 40000, 200);
            return $this->update([
                'type' => $param['type']
            ], [
                'id' => $support['id']
            ]);
        }

        return self::create([
            'type' => $param['type'],
            'user_id' => $userId,
            'post_id' => $param['post_id']
        ]);
    }
}
